<?php
namespace Admin\Controller;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/28
 * Time: 9:45
 * @author lin <lt@ourstu.com>
 */
use Admin\Builder\AdminListBuilder;
use Admin\Builder\AdminConfigBuilder;
class OrderController extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    //todo 各项配置
    public function config(){
        $field = D('Ucenter/Score')->getTypeList(array('status' => 1));
        $configBuilder = new AdminConfigBuilder();
        $data = $configBuilder->handleConfig();

        $param = array();
        $param['opt'] = $field;
        $de_data = $data['PAY_FIELD'];
        $param['jsonData'] = $de_data;
        $param['data'] = json_decode($de_data, true);

        $recharge = array();
        $recharge['opt'] = $field;
        $re_data = $data['RE_FIELD'];
        $recharge['jsonData'] = $re_data;
        $recharge['data'] = json_decode($re_data, true);


        $withdraw = array();
        $withdraw['opt'] = $field;
        $wi_data = $data['WI_FIELD'];
        $withdraw['jsonData'] = $wi_data;
        $withdraw['data'] = json_decode($wi_data, true);


        $configBuilder->title('支付设置')->data($data)
            ->keyUserDefined('PAY_FIELD', '积分类型和积分的兑率设置','如填写100则表示1RMB=100积分', T('Order@Order/config'), $param)

            ->keyText('RECHARGE_MIN_AMOUNT', '最小充值金额')
            ->keyUserDefined('RE_FIELD', '积分类型和积分的兑率设置','如填写100则表示1RMB=100积分', T('Order@Order/config'), $withdraw)
            ->keyDefault('RECHARGE_MIN_AMOUNT',0)

            ->keyText('WITHDRAW_MIN_AMOUNT', '最小提现金额')
            ->keyUserDefined('WI_FIELD', '积分类型和积分的兑率设置','如填写100则表示1RMB=100积分', T('Order@Order/config'), $withdraw)
            ->keyDefault('WITHDRAW_MIN_AMOUNT',0)

        ->group('支付设置', 'PAY_FIELD')
        ->group('充值设置','RECHARGE_MIN_AMOUNT,RE_FIELD')
        ->group('提现设置', 'WITHDRAW_MIN_AMOUNT,WI_FIELD');
        $configBuilder->buttonSubmit()
            ->buttonBack();
        $configBuilder->display();
    }

    public function index(){
        $aPage=I('get.page',1,'intval');
        $orders=M('order_goods')->page($aPage,20)->select();
        $totalCount=M('order_goods')->count();
        foreach ($orders as &$v){
            $goods=D('Order/OrderGoods')->getGoodsById($v['id']);
            $v['goods_id']=$goods['name'];
            $v['goods_type']=$goods['cate'];
            $score=M('ucenter_score_type')->where(array('id'=>$v['field']))->getField('title');
            $v['field']=$score;
            if($score==''){
                $v['field']='无';
            }
            if($v['method']=="wechat"){
                $v['method']='微信支付';
            }else{
                $v['method']=M('ucenter_score_type')->where(array('id'=>$v['method']))->getField('title');;
            }
            switch ($v['process']){
                case -1:
                {
                    $v['process']='交易关闭';
                    break;
                }
                case 0:
                {
                    $v['process']='等待付款';
                    break;
                }
                case 1:
                {
                    $v['process']='交易完成';
                }
            }
        }
        unset($v);
        //dump($orders);exit;
        $builder=new AdminListBuilder();
        $builder->title('商品订单列表')
            ->keyId('id','订单编号')
            ->keyUid('uid','下单用户')
            ->keyText('goods_id','商品名称')
            ->keyText('goods_type','商品类型')
            ->keyText('field','交易字段')
            ->keyText('method','交易方式')
            ->keyText('amount','交易数额')
            ->keyBool('is_pay','是否已付款')
            ->keyText('process','订单流程')
            ->keyStatus('status','订单状态')
            ->keyCreateTime('create_time','创建时间')
            ->keyTime('pay_time','支付时间')
            ->data($orders)
            ->pagination($totalCount,20)
            ->display();
    }

    //充值订单
    public function rechargeList(){
        $aPage=I('get.page',1,'intval');
        $orders=M('order_recharge')->page($aPage,20)->select();
        $totalCount=M('order_recharge')->count();
        foreach ($orders as &$v){
            $score=M('ucenter_score_type')->where(array('id'=>$v['field']))->getField('title');
            $v['field']=$score;
            if($v['method']=="wechat"){
                $v['method']='微信支付';
            }
            switch ($v['process']) {
                case -1: {
                    $v['process'] = '交易关闭';
                    break;
                }
                case 0: {
                    $v['process'] = '等待付款';
                    break;
                }
                case 1: {
                    $v['process'] = '交易完成';
                }
            }
        }
        unset($v);
        $builder=new AdminListBuilder();
        $builder->title('充值订单列表')
            ->keyId('id','订单编号')
            ->keyUid('uid','用户')
            ->keyText('field','充值字段')
            ->keyText('amount','充值数额')
            ->keyText('method','充值方式')
            ->keyBool('is_pay','是否已付款')
            ->keyText('process','订单流程')
            ->keyStatus('status','订单状态')
            ->keyCreateTime('create_time','创建时间')
            ->keyTime('pay_time','支付时间')
            ->data($orders)
            ->pagination($totalCount,20)
            ->display();
    }

    //todo 提现账户信息
    public function withdrawList(){
        $aPage=I('get.page',1,'intval');
        $orders=M('order_withdraw')->page($aPage,20)->select();
        $totalCount=M('order_withdraw')->count();
        foreach ($orders as &$v){
            $score=M('ucenter_score_type')->where(array('id'=>$v['field']))->getField('title');
            $v['field']=$score;
            if($v['method']=="wechat"){
                $v['method']='微信';
            }
            if ($v['pay_uid'] != 0) {
                $user = query_user(array('space_link'), $v['pay_uid']);
                $v['operator'] = $user['space_link'];
            } else {
                $v['operator'] = '-';
            }
            switch ($v['process']) {
                case -1: {
                    $v['process'] = '提现失败';
                    break;
                }
                case 0: {
                    $v['process'] = '提现中';
                    break;
                }
                case 1: {
                    $v['process'] = '提现完成';
                }
            }
        }
        $builder=new AdminListBuilder();
        $builder->title('提现记录')
            ->keyId()
            ->keyUid('uid','用户')
            ->keyText('field','提现字段')
            ->keyText('amount','提现金额')
            ->keyText('method','提现方式')
            ->keyText('process','提现流程')
            ->keyText('operator','操作者')
            ->keyText('account_info','收款账户信息')
            ->keyCreateTime('create_time','创建时间')
            ->keyTime('pay_time','提现完成时间')
            ->data($orders)
            ->pagination($totalCount,20)
            ->display();
    }
}